package me.zhengjie.modules.common.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import me.zhengjie.modules.common.domain.TEngineerMoney;
import me.zhengjie.modules.common.repository.TEngineerMoneyRepository;
import me.zhengjie.modules.common.service.TEngineerMoneyService;
import me.zhengjie.modules.common.service.dto.TEngineerMoneyDTO;
import me.zhengjie.modules.common.service.dto.TEngineerMoneyQueryCriteria;
import me.zhengjie.modules.common.service.mapper.TEngineerMoneyMapper;
import me.zhengjie.modules.customer.domain.TCustomerContract;
import me.zhengjie.modules.serviceprovider.domain.TServiceproviderContract;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * @author lhd
 * @date 2020-04-25
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TEngineerMoneyServiceImpl implements TEngineerMoneyService {

    @Autowired
    private TEngineerMoneyRepository tEngineerMoneyRepository;

    @Autowired
    private TEngineerMoneyMapper tEngineerMoneyMapper;

    @Override
    public Object queryAll(TEngineerMoneyQueryCriteria criteria, Pageable pageable) {
        Page<TEngineerMoney> page = tEngineerMoneyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tEngineerMoneyMapper::toDto));
    }

    @Override
    public Object queryAll(TEngineerMoneyQueryCriteria criteria){
        return tEngineerMoneyMapper.toDto(tEngineerMoneyRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public TEngineerMoneyDTO findById(Long id) {
        Optional<TEngineerMoney> tEngineerMoney = tEngineerMoneyRepository.findById(id);
        ValidationUtil.isNull(tEngineerMoney,"TEngineerMoney","id", id);
        return tEngineerMoneyMapper.toDto(tEngineerMoney.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TEngineerMoneyDTO> create(List<TEngineerMoney> resources) {
        List<TEngineerMoneyDTO> list = new ArrayList<>();
        resources.forEach((item) -> list.add(tEngineerMoneyMapper.toDto(tEngineerMoneyRepository.save(item))));
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(List<TEngineerMoney> resources) {
        resources.forEach((item)-> {
            Optional<TEngineerMoney> optionalTEngineerMoney = tEngineerMoneyRepository.findById(item.getId());
            ValidationUtil.isNull(optionalTEngineerMoney, "TEngineerMoney", "id", item.getId());

            TEngineerMoney tEngineerMoney = optionalTEngineerMoney.get();

            item.setId(tEngineerMoney.getId());
            tEngineerMoneyRepository.save(item);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<TEngineerMoney> resources) {
        resources.forEach((item) -> {
            tEngineerMoneyRepository.delete(item);
        });
    }

    private Long getCost(JSONArray array, String major, String level) {
        for(Object object: array) {
            JSONObject obj = (JSONObject)object;
            if(obj.getString("engineerType").equals(major) && obj.getString("engineerLevel").equals(level)) {
                if(!StringUtils.isEmpty(obj.getString("engineerCost"))) {
                    return obj.getLong("engineerCost");
                }
            }
        }
        return null;
    }

    @Override
    public Long getEngineerConst(TCustomerContract contract, String major, String level) {
        if(!StringUtils.isEmpty(contract.getEngineerMoney())) {
            JSONArray array = JSONArray.parseArray(contract.getEngineerMoney());
            return getCost(array,major,level);

        }
        return null;
    }

    @Override
    public Long getEngineerConst(TServiceproviderContract contract, String major, String level) {
        if(!StringUtils.isEmpty(contract.getEngineerMoney())) {
            JSONArray array = JSONArray.parseArray(contract.getEngineerMoney());
            return getCost(array,major,level);

        }
        return null;
    }
}
